package dawn.net.protocol;

import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.mina.core.session.IoSession;

import dawn.net.Message;
import dawn.service.socket.SessionAttribute;

public class JsonDecoder implements IDecoder {

    @SuppressWarnings("unchecked")
    @Override
    public void decode(IoSession session, String str) {

        Message msg = null;
        try {
//            msg = JSON.toJavaObject(JSON.parseObject(str), Message.class);

            // 发送过来的数据为空
            if (msg == null) {
                session.close(false);
                return;
            }

        } catch (Exception e) {
            // 收到的消息有错误
            session.close(false);
            return;
        }

        System.out.println("received：" + session.getId() + " - " + str);
        ConcurrentLinkedQueue<Message> queue = (ConcurrentLinkedQueue<Message>) session
                .getAttribute(SessionAttribute.MESSAGES);
        queue.add(msg);
    }

}
